// -----------------------------------------------------------------------
// <copyright file="DanhSachDonThuocData.cs" company="QSoft Viet Nam">
// TODO: Copyright © 2005-2012 QSoft Vietnam Corporation.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

    /// <summary>
    /// The class DanhSachDonThuocData
    /// </summary>
    public class DanhSachDonThuocDataAccess
    {
        /// <summary>
        /// The method support insert new DanhSachDonThuocEntity into database
        /// </summary>
        /// <param name="entity">DanhSachDonThuocEntity</param>
        /// <returns>Identify of new DanhSachDonThuocEntity</returns>
        public bool InsertDanhSachDonThuoc(DanhSachDonThuocEntity obj)
        {
            try
            {
                string sql = "spDanhSachDonThuoc_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, obj.MaDonThuoc);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "TenDonThuoc", DbType.String, obj.TenDonThuoc);
                db.AddInParameter(cmd, "LoiDan", DbType.String, obj.LoiDan);
                db.AddInParameter(cmd, "MaBacSy", DbType.String, obj.MaBacSy);
                int result = db.ExecuteNonQuery(cmd);

                return (result > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method support to update
        /// </summary>
        /// <param name="entity">DanhSachDonThuocEntity</param>
        /// <returns>true or false</returns>
        public bool EditDanhSachDonThuoc(DanhSachDonThuocEntity obj)
        {
            try
            {
                string sql = "spDanhSachDonThuoc_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, obj.MaDonThuoc);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "TenDonThuoc", DbType.String, obj.TenDonThuoc);
                db.AddInParameter(cmd, "LoiDan", DbType.String, obj.LoiDan);
                db.AddInParameter(cmd, "MaBacSy", DbType.String, obj.MaBacSy);
                int result = (int)db.ExecuteNonQuery(cmd);

                return (result > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method remove a DanhSachDonThuoc
        /// </summary>
        /// <param name="Id">DanhSachDonThuoc Id</param>
        /// <returns>true or false</returns>
        public bool DeleteDanhSachDonThuoc(string MaDonThuoc)
        {
            try
            {
                string sql = "spDanhSachDonThuoc_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.Int32, MaDonThuoc);

                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method support to get a DanhSachDonThuocEntity
        /// </summary>
        /// <returns>DanhSachDonThuocEntity</returns>
        public DanhSachDonThuocEntity GetDanhSachDonThuoc(string MaDonThuoc)
        {
            try
            {
                DanhSachDonThuocEntity obj = null;
                string sql = "spDanhSachDonThuoc_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaDonThuoc", DbType.String, MaDonThuoc);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DanhSachDonThuocEntity();
                        obj.MaDonThuoc = reader["MaDonThuoc"].ToString();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.TenDonThuoc = reader["TenDonThuoc"].ToString();
                        obj.LoiDan = reader["LoiDan"].ToString();
                        obj.MaBacSy = reader["MaBacSy"].ToString();
                    }
                }

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// The method support to get all DanhSachDonThuocEntity
        /// </summary>
        /// <returns>list of DanhSachDonThuocEntity</returns>
        public List<DanhSachDonThuocEntity> GetDanhSachDonThuocList()
        {
            try
            {
                List<DanhSachDonThuocEntity> list = new List<DanhSachDonThuocEntity>();
                DanhSachDonThuocEntity obj = null;
                string sql = "spDanhSachDonThuoc_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DanhSachDonThuocEntity();
                        obj.MaDonThuoc = reader["MaDonThuoc"].ToString();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.TenDonThuoc = reader["TenDonThuoc"].ToString();
                        obj.LoiDan = reader["LoiDan"].ToString();
                        obj.MaBacSy = reader["MaBacSy"].ToString();
                        list.Add(obj);
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
